<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>PC轮播图列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">

    <style>
        .layui-hide{  /*最后的pic为字段的field*/
            height: 100%;
            max-width: 100%;
        }
        .layui-table-cell{
            height:auto !important;
        }
    </style>
</head>
<body>


<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">PC轮播图列表</div>
                <div class="layui-card-body">


                    <div class="layui-form-item layui-form">
                        <div class="layui-btn-group test-table-operate-btn">
                            <button class="layui-btn" data-type="addadmin">添加轮播图</button>
                        </div>

                    </div>

                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate" lay-size="lg">

                        <script type="text/html" id="is_use_switch">
                            <input type="checkbox" name="is_use" lay-skin="switch" lay-text="启用|禁用" lay-filter="is_use_switch" value="{{ d.is_display }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.is_display == 0 ? 'checked' : '' }}>
                        </script>

                    </table>

                    <script type="text/html" id="test-table-operate-barDemo">

                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<!--添加弹窗-->
<div id="addmodal" style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">

        <div class="layui-form-item">
            <label class="layui-form-label">轮播图照片</label>
            <div class="layui-input-block">
                <input type="hidden" name="banner_img" id="banner_img">
                <button type="button" class="layui-btn" id="guide_card_thumb1">轮播图照片</button>

            </div>
            <div  class="layui-input-block" id="upload_thumb_img_div1" style="display: none;">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="upload_thumb_img1" style="width:200px;height:140px;">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">权重</label>
            <div class="layui-input-block">
                <input type="text" name="banner_sort" id="banner_sort" placeholder="请输入权重" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">轮播图链接</label>
            <div class="layui-input-block">
                <input type="text" name="banner_link" id="banner_link" placeholder="广告链接" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addsubmit">立即提交</button>

                </div>
            </div>
        </div>
    </form>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set','upload'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,upload = layui.upload
            ,laydate = layui.laydate
            ,form = layui.form;

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Banner/bannerpc')}"
            ,height: 'full-155'
            ,cols: [[
                {field:'banner_id',width:80,align:"center",  title: 'ID'}
                ,{field:'banner_img',align:"center",title: '广告图片',width:230,style:"height:150px;",templet:function(d){
                        return "<img src='" +d.banner_img+ "' style='display:block;width:205px;height:123px;'>"
                    }}
                ,{field:'banner_link',align:"center",title: '广告链接'}
                ,{field:'banner_sort',align:"center",title: '权重(双击可修改)',edit: 'text',style:'cursor: pointer;'}
                ,{field:'is_display',width:100, align:'center', title: '是否使用',templet: '#is_use_switch'}
                ,{align:'center',width:350, title: '操作',fixed: 'right', toolbar: '#test-table-operate-barDemo'}
            ]]
            ,page: true
            ,limit: 20
        });

        form.render(null, 'component-form-group');

        laydate.render({
            elem: '#LAY-component-form-group-date'
        });

        var uploadInst = upload.render({
            elem: '#guide_card_thumb1'
            ,url: "{:url('Common/upload')}"
            ,before: function(obj){
                $('#upload_thumb_img_div1').show();
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#upload_thumb_img1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){

                if (res.code == 1){
                    $('#banner_img').val(res.data);
                    return layer.msg(res.msg);
                }else{
                    return layer.msg(res.msg);
                }
            }
        });

        //监听单元格编辑
        table.on('edit(test-table-operate)', function(obj) {
            console.log(obj);
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var reg = /^\d{1,4}$/;
            if (!reg.test(obj.value)) {
                layer.close(loading);
                layer.msg('请输入1-4位数字',{icon:2,time:1500},function(){
                    window.location.reload();
                });

                return false;
            }
            $.ajax({
                url: "{:url('Banner/editSortPc')}",
                type: 'post',
                dataType: 'json',
                data: {id:obj.data.banner_id,sort:obj.value},
                success: function(msg){
                    layer.close(loading);
                    if (msg.code == "0") {
                        layer.close(loading);
                    }else{
                        layer.close(loading);
                        layer.msg(msg.code,{icon:2,time:1500},function(){
                            window.location.reload();
                        });
                    }

                }
            })

        });
        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                window.location.href="{:url('Banner/editBannerpc')}"+'?banner_id='+data.banner_id;
            } else if(obj.event === 'del'){
                layer.confirm('确认删除吗？', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "{:url('Banner/bannerPcDel')}",
                        type: 'post',
                        dataType: 'json',
                        data: {id:data.banner_id},
                        success: function(msg){
                            layer.close(loading);
                            if (msg === "1") {
                                obj.del();
                                layer.close(index);
                            }else{
                                layer.msg('删除失败',{icon:2,time:1000});
                            }
                        }
                    })

                });
            }
        });

        //添加提交
        form.on('submit(addsubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Banner/bannerPcAdd')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(data){
                    if (data.code === 0) {
                        layer.msg('添加成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else if(data.code===1){
                        layer.close(loading);
                        layer.msg(data.msg,{icon:2,time:1000})
                    }
                    else{
                        layer.close(loading);
                        layer.msg(data.code,{icon:2,time:1000})
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //启用还是禁用
        form.on('switch(is_use_switch)', function(obj){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            json = table.clearCacheKey(json);

            var is_show = this.checked ? '0' : '1';

            $.ajax({
                url: "{:url('Banner/editPcIsShow')}",
                type: 'post',
                dataType: 'json',
                data: {id:json.banner_id,is_show:is_show},
                success: function(info){
                    if (info.code === 0) {
                        layer.close(loading);
                    }else{
                        layer.msg(info,{icon:2,time:1000},function(){
                            window.location.reload();
                        });
                    }
                }
            })
        });

        var $ = layui.$, active = {
            searchUserButton:function(){
                var ad_type  = $('#ad_type').val();
                table.reload('test-table-operate', {
                    where: {
                        type: ad_type
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                //return false;
            },
            addadmin: function() {
                var addmodal = layer.open({
                    title: '添加轮播图'
                    , type: 1
                    , shadeClose: true
                    ,zIndex:10
                    , content: $('#addmodal')
                });
                layer.full(addmodal);
            }
        };
        //绑定点击事件
        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });



</script>


</body>
</html>